package com.xiyun.admin.controller;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyun.common.response.result.R;
import com.xiyun.admin.entity.Teacher;
import com.xiyun.admin.entity.vo.TeacherQuery;
import com.xiyun.admin.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Api(description = "讲师管理")
@RestController
@RequestMapping("/xiyun/service/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 测试后端服务连接使用
     * @return  返回RespondBody = success
     */
    @ApiOperation("测试-获取消息")
    @ResponseBody
    @GetMapping("getMsg")
    public R getMsg(){
        return R.ok().message("成功");
    }

    /**
     * 从数据库读取所有讲师信息
     * @return  返回讲师列表
     */
    @ApiOperation(value = "获取讲师列表")
    @GetMapping("getTeacherList")
    public R getTeacherList(){
        List<Teacher> teacherList = teacherService.list(null);
        return R.ok().data("teachers",teacherList);
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        return save ? R.ok() : R.error();
    }

    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacherById/{id}")
    public R editTeacher(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @ApiOperation("修改讲师")
    @PutMapping("updateTeacher")
    public R editTeacher(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        return b ? R.ok() : R.error();
    }

    @ApiOperation("组合条件分页查询讲师信息")
    @PostMapping("queryTeacher/{current}/{size}")
    public R queryTeacher(
            @ApiParam(name = "current",value = "当前页",required = true)
            @PathVariable long current,
            @ApiParam(name = "size",value = "单页行数",required = true)
            @PathVariable long size,
            @RequestBody
            TeacherQuery teacherQuery){

        //创建对象
        Page<Teacher> page = new Page<>(current,size);
        //条件查询
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        //获取信息
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String dept = teacherQuery.getDept();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //构建条件
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(dept)){
            wrapper.eq("dept",dept);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        //按时间排序
        wrapper.orderByDesc("gmt_create");

        teacherService.page(page,wrapper);
        //总记录数
        long total = page.getTotal();
        //页面数据
        List<Teacher> records = page.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("删除讲师")
    @GetMapping("deleteTeacher/{id}")
    public R delTeacher(@PathVariable String id){
        boolean b = teacherService.removeById(id);
        return b ? R.ok() : R.error();
    }

}
